package com.rc.saas.tenant.controller.msg;

import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.base.WebPageInfo;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.model.msg.TenantCustomMsg;
import com.rc.saas.tenant.model.tenant.TenantUser;
import com.rc.saas.tenant.search.tenant.TenantCustomMsgSearch;
import com.rc.saas.tenant.service.msg.TenantCustomMsgService;
import com.rc.saas.tenant.shiro.ShiroUtils;
import com.rc.saas.tenant.vo.msg.TenantCustomMsgResultVo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author frog
 * @date 2019/12/17
 */
@Controller
@RequestMapping("/tenantCustomMsg")
public class TenantCustomMsgController {
    private static Logger logger = LogManager.getLogger(TenantCustomMsgController.class);
    @Autowired
    private TenantCustomMsgService tenantCustomMsgService;

    /**
     * 跳转客户消息页面
     *
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String customMsgPage() {
        return "/templates/msg/tenantCustomMsg";
    }

    /**
     * 返回客户消息列表数据
     *
     * @return
     */
    @RequestMapping(value = "/listTenantCustomMsg", method = RequestMethod.GET)
    @ResponseBody
    public WebPageInfo listTenantCustomMsg(TenantCustomMsgSearch tenantCustomMsgSearch) {
        PageInfo<TenantCustomMsgResultVo> pageInfo = tenantCustomMsgService.searchByCondition(tenantCustomMsgSearch);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }


    /**
     * 跳转聊天记录页面
     *
     * @return
     */
    @RequestMapping(value = "/chatRecord/{customMsgId}", method = RequestMethod.GET)
    public String chatRecordPage(@PathVariable("customMsgId") Long customMsgId, Model model) {
        TenantCustomMsg tenantCustomMsg = tenantCustomMsgService.selectByPrimaryKey(customMsgId);
        //设置为已读
        tenantCustomMsg.setIsRead("1");
        tenantCustomMsgService.updateByPrimaryKeySelective(tenantCustomMsg);
        model.addAttribute("memberId", tenantCustomMsg.getMemberId());
        return "/templates/msg/chatRecord";
    }

    /**
     * 返回聊天记录列表数据
     *
     * @return
     */
    @RequestMapping(value = "/listChatRecordPage", method = RequestMethod.GET)
    @ResponseBody
    public WebPageInfo listChatRecordPage(TenantCustomMsgSearch tenantCustomMsgSearch) {
        PageInfo<TenantCustomMsgResultVo> pageInfo = tenantCustomMsgService.searchByCondition(tenantCustomMsgSearch);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }

    /**
     * 客服给会员发送消息
     *
     * @return
     */
    @RequestMapping(value = "/sendMessage", method = RequestMethod.POST)
    @ResponseBody
    public TResponse sendMessage(Long memberId, String content) {
        TenantUser user = ShiroUtils.getSessionUser();
        try {
            tenantCustomMsgService.insertMessage(memberId,content,user);
            return TResponse.SUCCESS();
        } catch (Exception e) {
            logger.error("发送消息失败", e);
            return TResponse.FAIL("操作失败");
        }
    }

}
